/**
 * Created by Admin on 2016/11/26.
 */
var seckill = {
    URL : {
        now : function () {
            return '/seckill/time/now';
        },
        exposer : function (seckillId) {
            return '/seckill/' + seckillId + '/exposer';
        },
        execution : function (seckillId, md5) {
            return '/seckill/' + seckillId + '/' + md5 + '/execution';
        }
    },
    handleSeckillKill : function (seckillId, node) {
        // 获取秒杀地址，控制显示逻辑，执行秒杀
        node.hide().
            html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');
        $.post(seckill.URL.exposer(seckillId), {}, function (result) {
            // 在回调函数中，执行交互流程
            if (result && result['success']) {
                var exposer = result['data'];
                if (exposer['exposed']) {
                    // 开启秒杀
                    // 获取秒杀地址
                    var md5 = exposer['md5'];
                    var killUrl = seckill.URL.execution(seckillId, md5);
                    $('#killBtn').one('click', function () {
                        // 执行秒杀请求
                        // 禁用按钮
                        $(this).addClass('disabled');
                        console.log(killUrl);
                        $.post(killUrl, {}, function (result) {
                            if (result && result['success']) {
                                var killResult = result['data'];
                                var state = killResult['state'];
                                var stateInfo = killResult['stateInfo'];
                                node.html('<span class="label label-success">' + stateInfo + '</span>');
                            }
                        })
                    });
                    node.show();
                } else {
                    // 未开启秒杀
                    var now = exposer['now'];
                    var start = exposer['start'];
                    var end = exposer['end'];
                    // 重新进入计时逻辑
                    seckill.countdown(seckillId, now, start, end);
                }
            } else {

            }
        })
    },
    // 验证手机号
    validatePhone : function (phone) {
        return !!(phone && phone.length == 11 && !isNaN(phone));
    },
    countdown : function (seckillId, nowDate, startDate, endDate) {
        var seckillBox = $('#seckill-box');
        // 时间判断
        if (nowDate > endDate) {
            seckillBox.html('秒杀结束');
        } else if (nowDate < startDate) {
            // 秒杀未开始，计时时间绑定
            var killDate = new Date(startDate + 1000);
            seckillBox.countdown(killDate, function (event) {
                var format = event.strftime('秒杀倒计时: %D天 %H时 %M分 %S秒');
                seckillBox.html(format);
            }).on('finish.countdown', function () {
                seckill.handleSeckillKill(seckillId, seckillBox);
            });
        } else {
            // 秒杀开始
            seckill.handleSeckillKill(seckillId, seckillBox);
        }
    },
    // 详情页秒杀逻辑
    detail:{
        // 详情页初始化
        init : function (params) {
            // 手机验证和登录，计时交互
            var killPhone = $.cookie("killPhone");
            var startDate = params['startDate'];
            var endDate = params['endDate'];
            var seckillId = params['seckillId'];
            // 验证手机号
            if (!seckill.validatePhone(killPhone)) {
                var killPhoneModal = $('#killPhoneModal');
                killPhoneModal.modal({
                    show: true,//显示弹出层
                    backdrop: 'static', //禁止位置关闭
                    keyboard: false //禁止键盘时间
                });
                $('#killPhoneBtn').click(function () {
                    var inputPhone = $('#killPhoneKey').val();
                    if (seckill.validatePhone(inputPhone)) {
                        $.cookie('killPhone', inputPhone, {expires: 7, path: '/seckill'});
                        window.location.reload();
                    } else {
                        $('#killPhoneMessage').hide().html('<label class="label label-danger">手机号错误！</label>>').show(300);
                    }
                });
            }
            // 已经登录
            // 计时交互
            $.get(seckill.URL.now(), {}, function (result) {
                if (result && result['success']) {
                    var nowDate = result['data'];
                    // 时间判断
                    seckill.countdown(seckillId, nowDate, startDate, endDate);
                } else {

                }
            })
        }
    }
};